import { systemPreviewFile } from "@/api/file";
import exportBlob from "@/util/exportBlob";
export default {
  methods: {
    typesMixin(value) {
      const typeImg = [
        "svgz",
        "pjp",
        "png",
        "ico",
        "avif",
        "tiff",
        "tif",
        "jfif",
        "svg",
        "xbm",
        "pjpeg",
        "webp",
        "jpg",
        "jpeg",
        "bmp",
        "gif",
      ];
      const typeVideo = [
        "avi",
        "wmv",
        "mpg",
        "mpeg",
        "mov",
        "rm",
        "ram",
        "swf",
        "flv",
        "mp4",
        "mp3",
        "wma",
        "avi",
        "rm",
        "rmvb",
        "flv",
        "mpg",
        "mkv",
      ];
      const typePdf = ["pdf"];
      const map = [
        [() => typeImg.includes(value), () => "img"],
        [() => typeVideo.includes(value), () => "video"],
        [() => typePdf.includes(value), () => "pdf"],
      ];
      const target = map.find((item) => item[0]());
      return target ? target[1]() : null;
    },
    async onPreviewMixin(id) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve) => {
        const { data, headers } = await systemPreviewFile({ id }, { responseType: "blob" });
        const contentType = headers["content-type"];
        if (contentType) {
          const blob = new Blob([data], { type: contentType }); //指定格式为vnd.ms-excel
          const href = window.URL.createObjectURL(blob); //创建下载的链接
          const type = this.typesMixin(contentType.split("/")[1]);
          const resolveData = {
            type,
            href,
          };
          resolve(resolveData);
        } else {
          try {
            await exportBlob({ params: { id } });
          } catch (error) {
            console.log(error);
          }
        }
      });
    },
  },
};
